using Services;
using System.Collections.Generic;
using UnityEngine;

public class IndicatorPoint : MonoBehaviour
{
    private static Dictionary<EEnemyState, Color> dict_color;
    static IndicatorPoint()
    {
        dict_color = new Dictionary<EEnemyState, Color>()
        {
            {EEnemyState.Patrol,Color.white },
            {EEnemyState.Sleep,Color.white },
            {EEnemyState.SawPlayer,Color.yellow },
            {EEnemyState.SeePlayer,Color.red }
        };
    }

    private MyObject myObject;
    private SpriteRenderer m_spriteRenderer;
    private Enemy enemy;

    private void Awake()
    {
        myObject = GetComponent<MyObject>();
        m_spriteRenderer = GetComponent<SpriteRenderer>();
        myObject.OnActivate += OnActivate;
        myObject.OnRecycle += OnRecycle;
    }

    private void OnActivate()
    {
        enemy = GetComponentInParent<Enemy>();
        enemy.StateChange += OnEnemyStateChange;
        enemy.VisibleDetector.VisibleChange += OnVisibleChange;
        m_spriteRenderer.color = dict_color[enemy.State];
    }

    private void OnRecycle()
    {
        enemy.StateChange -= OnEnemyStateChange;
        enemy.VisibleDetector.VisibleChange -= OnVisibleChange;
    }

    private void OnEnemyStateChange(EEnemyState state)
    {
        m_spriteRenderer.color = dict_color[state];
    }

    private void OnVisibleChange(bool value)
    {
        m_spriteRenderer.enabled = value;
    }
}
